package com.ruoyi.traveladmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.traveladmin.domain.TaTravel;
import com.ruoyi.traveladmin.mapper.TaTravelMapper;
import com.ruoyi.traveladmin.service.ITaTravelService;
import com.ruoyi.useradmin.service.IUaUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TaTravelServiceImpl implements ITaTravelService {


    @Resource
    private TaTravelMapper taTravelMapper;
    
    
    @Resource
    private IUaUserService uaUserService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param taTravel 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<TaTravel> getTaTravelPage(PageDomain pageDomain, TaTravel taTravel) {
        if (Objects.isNull(taTravel)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<TaTravel> queryWrapper = new LambdaQueryWrapper<>(new TaTravel());
            //if(!Objects.isNull(taTravel.getName())){
            //	queryWrapper.like(TaTravel::getName,taTravel.getName());
            //}
            

            if (!Objects.isNull(taTravel.getId())) {
                queryWrapper.eq(TaTravel::getId, taTravel.getId());
            }

            if (!Objects.isNull(taTravel.getUaUserId())) {
                queryWrapper.eq(TaTravel::getUaUserId, taTravel.getUaUserId());
            }

            if (!Objects.isNull(taTravel.getCity())) {
                queryWrapper.like(TaTravel::getCity, taTravel.getCity());
            }

            if (!Objects.isNull(taTravel.getPosition())) {
                queryWrapper.like(TaTravel::getPosition, taTravel.getPosition());
            }

            if (!Objects.isNull(taTravel.getTitle())) {
                queryWrapper.like(TaTravel::getTitle, taTravel.getTitle());
            }

            if (!Objects.isNull(taTravel.getLatitude())) {
                queryWrapper.like(TaTravel::getLatitude, taTravel.getLatitude());
            }

            if (!Objects.isNull(taTravel.getLongitude())) {
                queryWrapper.like(TaTravel::getLongitude, taTravel.getLongitude());
            }

            if (!Objects.isNull(taTravel.getPhotoUrl())) {
                queryWrapper.like(TaTravel::getPhotoUrl, taTravel.getPhotoUrl());
            }

            if (!Objects.isNull(taTravel.getPraiseNum())) {
                queryWrapper.like(TaTravel::getPraiseNum, taTravel.getPraiseNum());
            }

            if (!Objects.isNull(taTravel.getCollectionNum())) {
                queryWrapper.like(TaTravel::getCollectionNum, taTravel.getCollectionNum());
            }

            if (!Objects.isNull(taTravel.getStatus())) {
                queryWrapper.like(TaTravel::getStatus, taTravel.getStatus());
            }

            if (!Objects.isNull(taTravel.getDelFlag())) {
                queryWrapper.like(TaTravel::getDelFlag, taTravel.getDelFlag());
            }

            if (!Objects.isNull(taTravel.getCreateBy())) {
                queryWrapper.like(TaTravel::getCreateBy, taTravel.getCreateBy());
            }

            if (!Objects.isNull(taTravel.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravel.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravel.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravel::getCreateTime, taTravel.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravel.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravel::getCreateTime, taTravel.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravel.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravel.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravel::getCreateTime, taTravel.getParams().get("CreateTimeBeginTime"), taTravel.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravel.getUpdateBy())) {
                queryWrapper.like(TaTravel::getUpdateBy, taTravel.getUpdateBy());
            }

            if (!Objects.isNull(taTravel.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravel.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravel.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravel::getUpdateTime, taTravel.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravel.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravel::getUpdateTime, taTravel.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravel.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravel.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravel::getUpdateTime, taTravel.getParams().get("UpdateTimeBeginTime"), taTravel.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravel.getRemark())) {
                queryWrapper.like(TaTravel::getRemark, taTravel.getRemark());
            }
        Page<TaTravel> taTravelList= taTravelMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(TaTravel record:taTravelList.getRecords()){
            record.setUaUser(uaUserService.getUaUserById(record.getUaUserId()));
        }

            return taTravelList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public TaTravel getTaTravelById(Long id) {
        if (Objects.isNull(id)) {
            return new TaTravel();
        }
        try {
            TaTravel taTravel = taTravelMapper.selectById(id);
            taTravel.setUaUser(uaUserService.getUaUserById(taTravel.getUaUserId()));
            if (Objects.isNull(taTravel)){
                taTravel = new TaTravel();
            }
            return taTravel;

        } catch (Exception e) {
            e.printStackTrace();
            return new TaTravel();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param taTravel 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByTaTravel(TaTravel taTravel) {
        if (Objects.isNull(taTravel)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<TaTravel> queryWrapper = new LambdaQueryWrapper<>(new TaTravel());
            

            if (!Objects.isNull(taTravel.getId())) {
                queryWrapper.eq(TaTravel::getId, taTravel.getId());
            }

            if (!Objects.isNull(taTravel.getUaUserId())) {
                queryWrapper.eq(TaTravel::getUaUserId, taTravel.getUaUserId());
            }

            if (!Objects.isNull(taTravel.getCity())) {
                queryWrapper.like(TaTravel::getCity, taTravel.getCity());
            }

            if (!Objects.isNull(taTravel.getPosition())) {
                queryWrapper.like(TaTravel::getPosition, taTravel.getPosition());
            }

            if (!Objects.isNull(taTravel.getTitle())) {
                queryWrapper.like(TaTravel::getTitle, taTravel.getTitle());
            }

            if (!Objects.isNull(taTravel.getLatitude())) {
                queryWrapper.like(TaTravel::getLatitude, taTravel.getLatitude());
            }

            if (!Objects.isNull(taTravel.getLongitude())) {
                queryWrapper.like(TaTravel::getLongitude, taTravel.getLongitude());
            }

            if (!Objects.isNull(taTravel.getPhotoUrl())) {
                queryWrapper.like(TaTravel::getPhotoUrl, taTravel.getPhotoUrl());
            }

            if (!Objects.isNull(taTravel.getPraiseNum())) {
                queryWrapper.like(TaTravel::getPraiseNum, taTravel.getPraiseNum());
            }

            if (!Objects.isNull(taTravel.getCollectionNum())) {
                queryWrapper.like(TaTravel::getCollectionNum, taTravel.getCollectionNum());
            }

            if (!Objects.isNull(taTravel.getStatus())) {
                queryWrapper.like(TaTravel::getStatus, taTravel.getStatus());
            }

            if (!Objects.isNull(taTravel.getDelFlag())) {
                queryWrapper.like(TaTravel::getDelFlag, taTravel.getDelFlag());
            }

            if (!Objects.isNull(taTravel.getCreateBy())) {
                queryWrapper.like(TaTravel::getCreateBy, taTravel.getCreateBy());
            }

            if (!Objects.isNull(taTravel.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravel.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravel.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravel::getCreateTime, taTravel.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravel.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravel::getCreateTime, taTravel.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravel.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravel.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravel::getCreateTime, taTravel.getParams().get("CreateTimeBeginTime"), taTravel.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravel.getUpdateBy())) {
                queryWrapper.like(TaTravel::getUpdateBy, taTravel.getUpdateBy());
            }

            if (!Objects.isNull(taTravel.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravel.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravel.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravel::getUpdateTime, taTravel.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravel.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravel::getUpdateTime, taTravel.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravel.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravel.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravel::getUpdateTime, taTravel.getParams().get("UpdateTimeBeginTime"), taTravel.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravel.getRemark())) {
                queryWrapper.like(TaTravel::getRemark, taTravel.getRemark());
            }

            List<TaTravel> taTravels = taTravelMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravel taTravelNo:taTravels){
                idList.add(taTravelNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过uaUser外键idList查询idList
     *
     * @param uaUserIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByUaUserIdList(List<Long> uaUserIdList) {
        if (Objects.isNull(uaUserIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<TaTravel> queryWrapper = new LambdaQueryWrapper<>(new TaTravel());
            queryWrapper.in(TaTravel::getUaUserId, uaUserIdList);
            List<TaTravel> taTravels = taTravelMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravel taTravel:taTravels){
                idList.add(taTravel.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param taTravel 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertTaTravel(TaTravel taTravel) {
        if (Objects.isNull(taTravel)) {
            return -1;
        }
        try {
        
            //taTravel.setSpell(PinYin.getPinYin(taTravel.getName()));
            return taTravelMapper.insert(taTravel);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param taTravel 实体类
     * @return 结果
     */
    @Override
    public int updateTaTravelById(TaTravel taTravel) {
        if (Objects.isNull(taTravel)) {
            return -1;
        }
        try {
            //taTravel.setSpell(PinYin.getPinYin(taTravel.getName()));
            return taTravelMapper.updateById(taTravel);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteTaTravelByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return taTravelMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<TaTravel> getTaTravelList() {
        try {
            LambdaQueryWrapper<TaTravel> queryWrapper = new LambdaQueryWrapper<>(new TaTravel());
            queryWrapper.isNotNull(TaTravel::getId);
            return taTravelMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
